using System.Collections.Generic;
using UnityEngine;

public class CreateBonuseForTypeGame : MonoBehaviour
{
	private List<ListPointBonuseForGameType> listBonuses = new List<ListPointBonuseForGameType>();

	private List<BonuseItem> listBonuseItem = new List<BonuseItem>();

	public void Awake()
	{
		listBonuses = GetComponent<InfoPointForBonuseGameType>().listBonuses;
		listBonuseItem.AddRange(Resources.LoadAll<BonuseItem>("Bonuse/"));
	}

	private void Start()
	{
		if (PhotonNetwork.isMasterClient)
		{
			SpawnBonuse();
		}
	}

	private void SpawnBonuse()
	{
		List<PointForBonuse> list = new List<PointForBonuse>();
		foreach (ListPointBonuseForGameType listBonuse in listBonuses)
		{
			if (GameController.instance.curTypeGame == listBonuse.type)
			{
				list = listBonuse.listSpawnBonuse;
				break;
			}
		}
		foreach (PointForBonuse item in list)
		{
			GetComponent<InfoPointForBonuseGameType>().goWithCollider.transform.position = item.pos + Vector3.down;
			InstantBonuse(item.listBonuse, item.timeForRespawnBonuse, item.pos);
		}
		GetComponent<InfoPointForBonuseGameType>().goWithCollider.transform.position = Vector3.down * 1000f;
	}

	public void InstantBonuse(List<SpawnBonuse> listBonuse, float respawnTime, Vector3 posRespawnBonuse)
	{
		int sumAllProbability = 0;
		listBonuse.ForEach(delegate(SpawnBonuse x)
		{
			sumAllProbability += x.percent;
		});
		int num = Random.Range(0, sumAllProbability);
		int num2 = 0;
		foreach (SpawnBonuse item in listBonuse)
		{
			num2 += item.percent;
			if (num2 < num)
			{
				continue;
			}
			Dictionary<string, object> dictionary = new Dictionary<string, object>();
			object[] array = new object[1];
			int num3 = 0;
			foreach (SpawnBonuse item2 in listBonuse)
			{
				Dictionary<string, object> dictionary2 = new Dictionary<string, object>();
				dictionary2.Add("count", item2.count);
				dictionary2.Add("idProduct", item2.idProduct);
				dictionary2.Add("percent", item2.percent);
				dictionary2.Add("spawnType", item2.spawnType);
				dictionary.Add("k" + num3, dictionary2);
				num3++;
			}
			array[0] = dictionary;
			GameObject gameObject = CreateBonnuse(GetBonusItemForID(item.spawnType), posRespawnBonuse, array);
			BonuseItem component = gameObject.GetComponent<BonuseItem>();
			component.listSpawnBonus = listBonuse;
			component.changeBonuse = true;
			if (respawnTime > 0f)
			{
				component.respwnTime = respawnTime;
			}
			if (!item.idProduct.Equals(string.Empty))
			{
				component.idProduct = item.idProduct;
				component.ChangeTexture();
				if (item.count != 0)
				{
					component.countItemAddMin = item.count;
				}
			}
			BonuseItem bonuseItem = component.RandomBonuse();
			component.ChangeBonuse((int)bonuseItem.id, bonuseItem.idProduct, bonuseItem.countItemAddMin);
			break;
		}
	}

	private GameObject CreateBonnuse(BonuseItem bonuse, Vector3 pos, object[] info = null)
	{
		if (settings.offlineMode)
		{
			return (GameObject)Object.Instantiate(bonuse.gameObject, pos, Quaternion.identity);
		}
		return PhotonNetwork.InstantiateSceneObject("Bonuse/" + bonuse.gameObject.name, pos, Quaternion.identity, 0, info);
	}

	private BonuseItem GetBonusItemForID(idBonuse id)
	{
		foreach (BonuseItem item in listBonuseItem)
		{
			if (item.id.Equals(id))
			{
				return item;
			}
		}
		return null;
	}
}
